package br.pucminas.dao;

import java.util.List;

import javax.persistence.Query;

import br.pucminas.models.Autor;

public class AutorDAO extends GenericDAO<Long, Autor>{

	private static final long serialVersionUID = -7782017634525369009L;

	@SuppressWarnings("unchecked")
	public List<Autor> buscaAutores(){
		Query query = super.getEntityManager().createQuery("SELECT a FROM Autor a");
		List<Autor> autores = (List<Autor>) query.getResultList();
		return autores;
	}
	
	public Autor inserirAutor(Autor autor) throws Exception{
		super.getEntityManager().getTransaction().begin();
		super.getEntityManager().persist(autor);
		super.getEntityManager().getTransaction().commit();
		return autor;
	}
	
	public Autor atualizarAutor(Autor autor) throws Exception{
		super.getEntityManager().getTransaction().begin();
		super.getEntityManager().merge(autor);
		super.getEntityManager().getTransaction().commit();
		return autor;
	}
	
	public void excluirAutor(Autor autor) throws Exception{
		super.getEntityManager().getTransaction().begin();
		super.delete(autor);
		super.getEntityManager().getTransaction().commit(); 
	}
}

